<template>
	<view class="wrap">
		<view class="food-list" v-if="state.items.length > 0">
			<food-item v-for="(item, index) in state.items" :key="index" :item="item.food_data" @show="onShowFood"
						@addCart="onAddCart">
			</food-item>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText" color="#888888"></load-more>
		<!-- 菜品详情 -->
		<food-view ref="foodView" :item="foodData" @addCart="onAddCart" @close="onShowCart"></food-view>
		<!-- 快捷购物车 -->
		<quick-cart ref="quickCart"></quick-cart>
	</view>
</template>

<script>
	import {
		getLikeList
	} from '@/service/api/food.js'
	import Util from '@/common/util'
	import mixinsAuth from '@/mixins/auth.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				hasLoad: false,
				state: {
					items: []
				},
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadRecordList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadRecordList();
			}
		},
		onShareAppMessage(res) {
			console.log('onShareAppMessage', res)
			let shareData = this.$store.getters.shareData
		
			return {
				title: shareData.title,
				path: shareData.path,
				imageUrl: shareData.image
			}
		},
		methods: {
			loadRecordList(refresh) {
				console.log('loadRecordList', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getLikeList(this.page, 10).then(([err, res]) => {
					console.log('getLikeList', err, res)
					this.loadingType = -1
					this.hasLoad = true
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无记录，快去收藏吧'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 2
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {
		padding-bottom: 150upx;
	}

	.food-list {
		.food-item {
			padding: 30upx;
			border-bottom: 1upx solid #eee;
		}
	}
</style>
